const UrlParam = () => {
  let data = [];
  let index = {};
  let u = window.location.search.substr(1);
  if (u !== '') {
    let params = decodeURIComponent(u).split('&');
    for (let i = 0, len = params.length; i < len; i++) {
      if (params[i] !== '') {
        let p = params[i].split("=");
        if (p.length === 1 || (p.length === 2 && p[1] === '')) {
          data.push(['']);
          index[p[0]] = data.length - 1;
        } else if (typeof(p[0]) === 'undefined' || p[0] === '') {
          continue;
        } else if (typeof(index[p[0]]) === 'undefined') {
          data.push([p[1]]);
          index[p[0]] = data.length - 1;
        } else {
          data[index[p[0]]].push(p[1]);
        }
      }
    }
  }
  return {
    param: (o) => {
      try {
        return (typeof(o) === 'number' ? data[o][0] : data[index[o]][0]);
      } catch (e) {
      }
    },
    paramValues: (o) => {
      try {
        return (typeof(o) === 'number' ? data[o] : data[index[o]]);
      } catch (e) {}
    },
    hasParam: (paramName) => {
      return typeof(paramName) === 'string' ? typeof(index[paramName]) !== 'undefined' : false;
    },
    paramMap: () => {
      let map = {};
      try {
        for (let p in index) { map[p] = data[index[p]]; }
      } catch (e) {}
      return map;
    }
  }
};

export default UrlParam;
